Skip to content

Improve deferred sequence#27

Merged
Baptouuuu merged 5 commits into
developfrom
improve-deferred-sequence
Nov 9, 2024
Merged

Improve deferred sequence#27
Baptouuuu merged 5 commits into
developfrom
improve-deferred-sequence

Conversation

@Baptouuuu

Copy link
Copy Markdown
Member

Problem

For implementation convenience each step of a deferred Sequence holds in memory the values generated for its step. This means that the example below will keep in memory the arrays [1, 2, 3, 4], [2, 3, 4, 5] and [4, 6, 8, 10] as long as $sequence is referenced.

$sequence = Sequence::defer((static function() {
    yield from [1, 2, 3, 4];
})())
    ->map(static fn($i) => $i + 1)
    ->map(static fn($i) => $i * 2);
$sequence->toList(); // this memoize the values

This is wasteful as the first 2 arrays can't be accessed from outside the monad and has no purpose inside the monad.

The more a user compose its sequences the more internal copies it holds.

Solution

When composing the generators we use the initial generator if nobody references the Sequence that points to the generator.

If somebody references the Sequence we use the Accumulate iterator as the user may need to iterate over the intermediary values.

If the underlying Generator has been started we use the Accumulate iterator as well in order to have access to the first values produced by the Generator (since a Generator can't be rewinded).

@Baptouuuu Baptouuuu added the enhancement New feature or request label Nov 9, 2024
@Baptouuuu Baptouuuu self-assigned this Nov 9, 2024
@Baptouuuu
Baptouuuu merged commit afe8ac4 into develop Nov 9, 2024
@Baptouuuu
Baptouuuu deleted the improve-deferred-sequence branch November 9, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant